package org.jeecg.modules.demo.crm.service.impl;

import org.apache.shiro.SecurityUtils;
import org.checkerframework.checker.units.qual.A;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.crm.entity.CSubProject;
import org.jeecg.modules.demo.crm.entity.CSubProjectStatusLog;
import org.jeecg.modules.demo.crm.mapper.CSubProjectMapper;
import org.jeecg.modules.demo.crm.mapper.CSubProjectStatusLogMapper;
import org.jeecg.modules.demo.crm.service.ICSubProjectStatusLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Description: 子项状态变更表
 * @Author: jeecg-boot
 * @Date:   2025-05-27
 * @Version: V1.0
 */
@Service
public class CSubProjectStatusLogServiceImpl extends ServiceImpl<CSubProjectStatusLogMapper, CSubProjectStatusLog> implements ICSubProjectStatusLogService {

    @Autowired
    private CSubProjectStatusLogMapper cSubProjectStatusLogMapper;
    @Autowired
    private CSubProjectMapper cSubProjectMapper;


    @Transactional
    @Override
    public void updateStatus(String id, String status) {
        updateStatus(id, status, null);
    }

    @Transactional
    @Override
    public void updateStatus(String id, String status, String remark) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        String oldStatus = "";
        CSubProject cSubProject = cSubProjectMapper.selectById(id);
        if(cSubProject != null){
            oldStatus = cSubProject.getStatus();
        }
        CSubProjectStatusLog cSubProjectStatusLog = new CSubProjectStatusLog();
        cSubProjectStatusLog.setSubProjectId(id);
        cSubProjectStatusLog.setNewStatus(status);
        cSubProjectStatusLog.setOldStatus(oldStatus);
        cSubProjectStatusLog.setChangedBy(sysUser.getId());
        cSubProjectStatusLog.setRemark(remark);
        cSubProjectStatusLog.setChangeTime(new Date());
        cSubProjectStatusLogMapper.insert(cSubProjectStatusLog);
    }

    @Override
    public void autoStatus(String id, String oldStatus, String status, Date change_time) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        CSubProjectStatusLog cSubProjectStatusLog = new CSubProjectStatusLog();
        cSubProjectStatusLog.setSubProjectId(id);
        cSubProjectStatusLog.setNewStatus(status);
        cSubProjectStatusLog.setOldStatus(oldStatus);
        cSubProjectStatusLog.setChangedBy(sysUser.getId());
        cSubProjectStatusLog.setChangeTime(change_time);
        cSubProjectStatusLogMapper.insert(cSubProjectStatusLog);

    }

    @Override
    public void add(CSubProjectStatusLog cSubProjectStatusLogDto) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        CSubProject cSubProject = cSubProjectMapper.selectById(cSubProjectStatusLogDto.getSubProjectId());

        //查询旧的状态 删除
        cSubProjectStatusLogMapper.delBySubProjectIdNewStatus(cSubProject.getId(),cSubProjectStatusLogDto.getNewStatus());

        CSubProjectStatusLog cSubProjectStatusLog = new CSubProjectStatusLog();
        cSubProjectStatusLog.setSubProjectId(cSubProject.getId());
        cSubProjectStatusLog.setNewStatus(cSubProjectStatusLogDto.getNewStatus());
        cSubProjectStatusLog.setOldStatus(cSubProject.getStatus());
        cSubProjectStatusLog.setChangedBy(sysUser.getId());
        cSubProjectStatusLog.setRemark(cSubProjectStatusLogDto.getRemark());
        cSubProjectStatusLog.setChangeTime(cSubProjectStatusLogDto.getChangeTime());
        cSubProjectStatusLogMapper.insert(cSubProjectStatusLog);



        cSubProject.setStatus(cSubProjectStatusLog.getNewStatus());
        cSubProjectMapper.updateById(cSubProject);


    }
}
